package com.jiyinit.web.core.common.listener;

import com.jiyinit.web.core.common.constant.Constant;
import com.jiyinit.web.core.common.vo.CurrentUserVo;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;

/**
 * Created by lr on 2017/11/24.
 */
public class SessionListener implements HttpSessionListener {
    /**
     * 该HashMap以用户名-HttpSession对象存储一个账号只能被一个人登陆的信息。
     */
    public static HashMap<String,HttpSession> sessionMap = new HashMap<String,HttpSession>();

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent)
    {
        HttpSession session = httpSessionEvent.getSession();
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
    {

        HttpSession session = httpSessionEvent.getSession();

        delSession( session );
    }

    public static synchronized void delSession(HttpSession session)
    {
        if(session != null)
        {

            // 删除单一登录中记录的变量
            if(session.getAttribute( Constant.CURRENT_LOGIN_USER_NAME) != null)
            {
                CurrentUserVo user = (CurrentUserVo) session.getAttribute( Constant.CURRENT_LOGIN_USER_NAME );
                SessionListener.sessionMap.remove( user.getUserName() );
            }
        }
    }

    public static synchronized void delSessionByuserName(String userName){
        SessionListener.sessionMap.remove( userName );
    }
}
